package net.cmbt.okframe.okbanner;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import net.cmbt.okframe.R;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

public class OKBannerNormal extends LinearLayout {
    private View view;
    private BaseCoustomViewpager viewPager;
    private ArrayList<String> list;
    Context context;
    private Timer timer;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if ((int) msg.obj == 1) {
                int currentItem = viewPager.getCurrentItem();
                viewPager.setCurrentItem(++currentItem, true);
                if (sendAttributeListener != null) {
                    sendAttributeListener.sendCurrentPostion(currentItem % list.size());
                }
            }
        }
    };

    SendAttributeListener sendAttributeListener;
    public BannerListener bannerListener;
    boolean isAuto;
    int periodSecond;

    public OKBannerNormal(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        initData(context);
    }


    public OKBannerNormal(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        initData(context);
    }


    /**
     * 初始化view
     *
     * @param context
     */
    private void initView(Context context) {
        view = LayoutInflater.from(context).inflate(R.layout.banner_group, this);
        viewPager = (BaseCoustomViewpager) view.findViewById(R.id.okbanner_vp);
        viewPager.setScrollDuration(this, 200);
    }

    /**
     * 初始化数据
     *
     * @param context
     */
    private void initData(Context context) {
        list = new ArrayList<>();
        this.context = context;
    }

    /**
     * 传递监听
     *
     * @param sendAttributeListener
     */
    public void setSendAttributeListener(SendAttributeListener sendAttributeListener) {
        this.sendAttributeListener = sendAttributeListener;
    }


    /**
     * 绑定并设置数据
     *
     * @param data
     * @param object
     * @param isAuto
     * @param periodSecond
     */
    public OKBannerNormal buildBanner(ArrayList<String> data, Object object, boolean isAuto, int periodSecond) {
        this.isAuto = isAuto;
        this.periodSecond = periodSecond;
        if (data != null) {
            list.clear();
            list.addAll(data);
        } else {
            return this;
        }

        if (object instanceof OkIndicatorNormal) {
            OkIndicatorNormal oKBannerNormal = (OkIndicatorNormal) object;
            oKBannerNormal.setOkBannerClass(this);
        }
        BannerAdapter bannerAdapter = new BannerAdapter(list, this);
        viewPager.setAdapter(bannerAdapter);

        startAutoPlay();

        if (sendAttributeListener != null) {
            sendAttributeListener.sendTotalCount(list.size());
        }
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                sendAttributeListener.sendCurrentPostion(position % list.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        return this;
    }

    /**
     * 开始播放
     */
    public void startAutoPlay() {
        if (isAuto) {
            if (timer != null) {
                timer.cancel();
            }
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    Message message = new Message();
                    message.obj = 1;
                    handler.sendMessage(message);
                }
            }, periodSecond, periodSecond);
        }
    }

    /**
     * 暂停播放
     */
    public void stopAutoPlay() {
        if (isAuto) {
            if (timer != null) {
                timer.cancel();
            }
        }
    }


    public void setBannerListener(BannerListener bannerListener) {
        this.bannerListener = bannerListener;
    }
}
